package org.enwey.icarenew.Activity.News;

import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import org.enwey.icarenew.API.APIManager;
import org.enwey.icarenew.APPUtils.ToastUtil;
import org.enwey.icarenew.Activity.Base.BaseActivity;
import org.enwey.icarenew.R;
import java.util.Hashtable;


/**
 * Created by enwey on 2014/11/23.
 */
public class NewDetailActivity extends BaseActivity {
    private WebView mWebView;
    private String mStyleId;
    private String mNewId;
//    private ListView mListView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_newdetail);
        setUp();
        findView();
        CallAPINewDetail();
    }

    public void setUp() {
        setTitle("新闻详情");
        showLeftBtn();
        mStyleId = getIntent().getStringExtra("style_id");
        mNewId = getIntent().getStringExtra("new_id");
    }

    private void findView() {
        mWebView = (WebView) this.findViewById(R.id.activity_newdetail_web);
        mWebView.setWebViewClient(new MyWebViewClient());
    }

    private void CallAPINewDetail() {
        Hashtable<String, String> parameters = new Hashtable<>();
        parameters.put("style_id", mStyleId);
        parameters.put("new_id", mNewId);
        String path = APIManager.getInstance(this).newDetail(parameters);
        setDetailToWeb(mWebView,path);
    }

    private void setDetailToWeb(WebView mWeb, String path) {
        WebSettings settings = mWeb.getSettings();
        mWeb.loadUrl(path);
        settings.setJavaScriptEnabled(true);
        settings.setSupportZoom(false);
        settings.setBuiltInZoomControls(true);
        mWeb.setBackgroundColor(0);
        Drawable background = mWeb.getBackground();

        if (background != null) {
            background.setAlpha(0);
        }

        mWeb.setWebViewClient(new MyWebViewClient());
    }

    public class MyWebViewClient extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return super.shouldOverrideUrlLoading(view, url);
        }

        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            super.onPageStarted(view, url, favicon);
            showLoading();
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
            hideLoading();
        }

        @Override
        public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
            super.onReceivedError(view, errorCode, description, failingUrl);
            hideLoading();
            if (errorCode != 200) {
                ToastUtil.show(NewDetailActivity.this, "网络异常，请检查网路!");
                return;
            }
        }
    }
}
